﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using System.Data;
using DQYT.HTDZ.Web;
using DQYT.HTDZ.Common;
using DQYT.HTDZ.Model;
using Maticsoft.DBUtility;

namespace SkWeb.Handlers
{
    public partial class SchemeAuditHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string resultJson = string.Empty;

            int pageSize, pageIndex, recordCount;

            string op = Request.QueryString["op"];

            string format = "\"total\":{0}, \"rows\":{1}";

            TB_Workflow_Log_BLL workflowBLL = new TB_Workflow_Log_BLL();

            int.TryParse(Request.Form["rows"], out pageSize);
            int.TryParse(Request.Form["page"], out pageIndex);
            if (pageSize == 0) pageSize = 15;
            if (pageIndex == 0) pageIndex = 1;

            recordCount = 0;

            switch (op)
            {
                case "pass":
                    TB_Workflow_Log_Model passModel = new TB_Workflow_Log_Model();
                    string types = Request.Form["ty"];
                    //对应 TB_FA.SHENHE 字段值
                    if (types.Equals("second"))
                    {
                        passModel.FromStatus = int.Parse(Request.Form["fromstatus"]);
                    }
                    else if (types.Equals("third"))
                    {
                        //passModel.FromStatus = 1;
                        passModel.FromStatus = 2;
                    }
                    else if (types.Equals("fourth"))
                    {
                        passModel.FromStatus = 2;
                    }
                   
                    passModel.ToStatus = passModel.FromStatus + 1;

                    //对应 TB_FA.ID 字段值
                    passModel.ObjID = long.Parse(Request.Form["objid"]);

                    //操作名称：通过，退回
                    passModel.SH_OP_NAME = Request.Form["opname"];

                    //审核意见
                    passModel.SH_RMK = Request.Form["rmk"];

                    passModel.USE_CODE = Globals.CurrentUserInfo.USE_CODE;
                    passModel.USE_NAME = Globals.CurrentUserInfo.USE_NAME;
                    
                    if(workflowBLL.AddWorkflowLog(passModel))
                        resultJson = "{\"success\":true}";
                    else
                        resultJson = "{\"success\":false, \"msg\":\"服务出现异常 \"}";
                    break;
                case "back":
                    TB_Workflow_Log_Model backModel = new TB_Workflow_Log_Model();
                    backModel.FromStatus = int.Parse(Request.Form["fromstatus"]);
                    //if (backModel.FromStatus > 0)
                    //{
                    backModel.ToStatus = backModel.FromStatus - 1;
                    //}
                    
                    backModel.ObjID = long.Parse(Request.Form["objid"]);
                    backModel.SH_OP_NAME = Request.Form["opname"];
                    backModel.SH_RMK = Request.Form["rmk"];
                    backModel.USE_CODE = Globals.CurrentUserInfo.USE_CODE;
                    backModel.USE_NAME = Globals.CurrentUserInfo.USE_NAME;
                    if (workflowBLL.AddWorkflowLog(backModel))
                        resultJson = "{\"success\":true}";
                    else
                        resultJson = "{\"success\":false, \"msg\":\"服务出现异常 \"}";
                    break;
                case "getdone":
                    DataTable table = workflowBLL.GetDone(Globals.CurrentUserInfo.USE_CODE, pageIndex, pageSize, out recordCount);
                    resultJson = JsonHelper.ToJson(table);
                    resultJson = "{" + string.Format(format, recordCount,
                        string.IsNullOrWhiteSpace(resultJson) ? "[]" : resultJson) + "}";
                    break;
                case "getwait":
                    DataTable waitTable = workflowBLL.GetWait(Globals.CurrentUserInfo.USE_CODE, 
                        Globals.CurrentUserInfo.JSE_ID == 100000, pageSize, pageIndex,true, out recordCount);
                    resultJson = JsonHelper.ToJson(waitTable);
                    resultJson = "{" + string.Format(format, recordCount,
                        string.IsNullOrWhiteSpace(resultJson) ? "[]" : resultJson) + "}";
                    break;
                case "getwait1":
                    DataTable waitTable1 = workflowBLL.GetWait(Globals.CurrentUserInfo.USE_CODE,
                        Globals.CurrentUserInfo.JSE_ID == 100000, pageSize, pageIndex,false, out recordCount);
                    resultJson = JsonHelper.ToJson(waitTable1);
                    resultJson = "{" + string.Format(format, recordCount,
                        string.IsNullOrWhiteSpace(resultJson) ? "[]" : resultJson) + "}";
                    break;

                case "check":
                  string jingId = Request.Form["objid"];
                  string step = Request.Form["step"];
                  string strShr = "SHR1";
                  string shrName = "";
                  string sql = "select * from tb_fa where id="+jingId;
                  
                  if (step == "third")
                      strShr = "SHR2";
                       
                   DataTable dt= OracleHelper.Query(sql).Tables[0];
                   shrName = Convert.ToString( dt.Rows[0][strShr]);
 
                   if (String.IsNullOrEmpty(shrName))
                   {
                       sql = "update tb_fa set " + strShr + "= '" + Globals.CurrentUserInfo.USE_NAME + "' where id="+jingId;
                       OracleHelper.ExecuteSql(sql);
                   }

                   if (String.IsNullOrEmpty(shrName) || shrName == Globals.CurrentUserInfo.USE_NAME)
                   {
                       resultJson = "{\"success\":true}";
                   }
                   else
                   {
                       resultJson = "{\"success\":false, \"msg\":\"这口井正在被" + shrName + "审核 \"}";
                   }
                    break;
                case "getworkflowlog":
                default:
                    string objId = Request.QueryString["objid"];
                    DataTable logTable = workflowBLL.GetWorkFowLog(objId);
                    resultJson = JsonHelper.ToJson(logTable);
                    resultJson = "{" + string.Format(format, recordCount,
                        string.IsNullOrWhiteSpace(resultJson) ? "[]" : resultJson) + "}";
                    break;
            }

            Response.ContentType = "text/json";
            Response.Write(resultJson);
            Response.End();
        }
    }
}